/*
 * Created on 2021年11月29日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.pentaho.di.ui.spoon.cluster;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.pentaho.di.cluster.SlaveServer;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.laf.BasePropertyHandler;
import org.pentaho.di.ui.spoon.Spoon;
import org.yzbdl.lanius.core.quartz.entity.TaskCronInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 表格类，用于展示定时任务信息，并提供修改
 *
 * @author yzbdl@yzbdl.ac.cn
 * @version 1.0
 * @
 */
public class CronInfoTableViewer {
    //表格(TableViewer类)对象
    private TableViewer tableViewer;

    //cluster服务器
    private SlaveServer slaveServer;

    public CronInfoTableViewer(SlaveServer slaveServer) {
        this.slaveServer = slaveServer;
    }

    //定义这个open()方法.就是创建一个典型的SWT程序的步骤
    public void open() {
        //1.创建一个或者多个Shell(shell是程序的主窗口)
        Shell shell = new Shell();
        //2.设置shell的布局及图标.
        shell.setSize(570, 350);
        shell.setImage(new Image(null, BasePropertyHandler.getProperty("lanius_icon")));
        //设置shell的布局为FillLayout
        shell.setLayout(new FillLayout());
        shell.setText(BaseMessages.getString(Spoon.class, "SpoonSlave.QuartzManage.TableTitle"));
        //3.调用自定义的方法创建表格
        createTableViewer(shell);


        //4.设定内容器
        tableViewer.setContentProvider(new TableViewerContentProvider());
        //5.设定标签器
        tableViewer.setLabelProvider(new TableViewerLabelProvider());
        //6.用setInput输入数据(把List集合传进来)
        tableViewer.setInput(getCronInfo());


        //增加一个事件监听器，包含单击双击功能
        addListener();
        //增加ActionGroup对象，功能组
        MyActionGroup actionGroup = new MyActionGroup(tableViewer, slaveServer);
        //调用fillContextMenu方法将按钮注入到菜单对象中
        actionGroup.fillContextMenu(new MenuManager());

        //7.创建Shell中的组件(这个例子中没有加入组件,只有一个空窗口)
        shell.open();
    }

    /**
     * 创建一个表格
     */
    public void createTableViewer(Composite prarent) {
        //第一步:定义一个TableViewer对象.
        //同时在构造方法中定义其式样. 这里设置成可以多选(SWT.MULTI),
        //有水平滚动条(SWT.H_SCROLL),有垂直滚动条(SWT.V_SCROLL),
        //有边框(SWT.BORDER),可以整行选择(SWT.FULL_SELECTION)
        tableViewer = new TableViewer(prarent, SWT.MULTI | SWT.H_SCROLL
                | SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.FULL_SELECTION);


        //第二步:通过TableViewer中的Table对其布局.
        Table table = tableViewer.getTable();
        table.setHeaderVisible(true);//设置标头
        table.setLinesVisible(true);//显示表格线
        TableLayout tLayout = new TableLayout();//专用于表格的布局
        table.setLayout(tLayout);

        //第三步:建立TableViewer中的列
        tLayout.addColumnData(new ColumnWeightData(30));
        new TableColumn(table, SWT.NONE).setText(BaseMessages.getString(Spoon.class, "SpoonSlave.QuartzManage.TableColumn1"));

        tLayout.addColumnData(new ColumnWeightData(60));
        new TableColumn(table, SWT.NONE).setText(BaseMessages.getString(Spoon.class, "SpoonSlave.QuartzManage.TableColumn2"));

        tLayout.addColumnData(new ColumnWeightData(30));
        new TableColumn(table, SWT.NONE).setText(BaseMessages.getString(Spoon.class, "SpoonSlave.QuartzManage.TableColumn3"));

        tLayout.addColumnData(new ColumnWeightData(70));
        new TableColumn(table, SWT.NONE).setText(BaseMessages.getString(Spoon.class, "SpoonSlave.QuartzManage.TableColumn4"));

        tLayout.addColumnData(new ColumnWeightData(70));
        new TableColumn(table, SWT.NONE).setText(BaseMessages.getString(Spoon.class, "SpoonSlave.QuartzManage.TableColumn5"));
    }

    /**
     * 事件的监听器
     */
    public void addListener() {
        //双击事件的监听
        tableViewer.addDoubleClickListener(new IDoubleClickListener() {//IDoubleClickListener是一个接口
            @Override
            public void doubleClick(DoubleClickEvent event) {
//                IStructuredSelection selection = (IStructuredSelection) event.getSelection();
//                //得到记录的实体对象(要类型转换)
//                People people = (People) selection.getFirstElement();
//                //一个弹出提示框
//                MessageDialog.openInformation(null, BaseMessages.getString(Spoon.class, "Spoon.Dialog.WarningCreateNewSteps.Title"), people.getName());
            }
        });

        /*
         * 单击事件的监听
         */
        tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
            @Override
            public void selectionChanged(SelectionChangedEvent event) {
                // 如果要定义单击的事件的处理在这里写代码.......
            }
        });
    }

    /**
     * 获取定时任务json数据
     *
     * @return
     */
    public List<TaskCronInfo> getCronInfo() {
        List<TaskCronInfo> list = new ArrayList<>();
        try {
            //CronTaskInfoServlet
            String path = "/kettle/gettaskinfo";
            JSONArray myJsonArray = JSONUtil.parseArray(slaveServer.execService(path));
            list = JSONUtil.toList(myJsonArray, TaskCronInfo.class);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return list;
        }
    }
}